//
//  EPMStatuesViewModel.m
//  WeiBoByOC
//
//  Created by 马继鵬 on 17/3/28.
//  Copyright © 2017年 7TH. All rights reserved.
//

#import "EPMStatuesViewModel.h"
#import "EPMTextAttachment.h"


@implementation EPMStatuesViewModel

    - (instancetype)init{
        if (self = [super init]){
          
        }
        return self;
    }
- (void)setHomeModel:(EPMHomeModel *)homeModel
{
    _homeModel = homeModel;
    int verify = homeModel.user.verified;
    self.vrifyImg =  [self verifyImage:verify];
    
    int menberClass = homeModel.user.mbrank;
    self.mBrankImg = [self dealmBrank:menberClass];
    
    self.strSource = [self dealSource:homeModel.source];
}
- (NSString*)strDate{
    return [self dealCreatTime:self.homeModel.created_at];
}

- (NSString*)dealSource:(NSString*)source{
    if (![source containsString:@"\">"]){
        return @"来自: 那遥远的地方";
    }
    NSRange startRange = [source rangeOfString:@"\">"];
    NSRange endRange = [source rangeOfString:@"</"];
    NSRange range = NSMakeRange(startRange.location+2, endRange.location - startRange.location -2);
    NSString* sourceStr = [source substringWithRange:range];
                        
    return [NSString stringWithFormat:@"来自: %@",sourceStr];
    
}



//生成时间处理
- (NSString*)dealCreatTime:(NSDate*)creatDate{
    
    NSDateFormatter* df = [[NSDateFormatter alloc]init];
    //判断是否是本年
    BOOL isCurrentYear = [self isCurrentYear:creatDate];
    if (!isCurrentYear){
        df.dateFormat = @"yyyy年MM月dd日 hh:mm";
    }else{
        NSCalendar* currentDate = [NSCalendar currentCalendar];
        if ([currentDate isDateInToday:creatDate]){
            int s = [[NSDate date] timeIntervalSinceDate:creatDate];
            if (s < 60){
                return @"刚刚";
            }else if( s>= 60 && s < 3600 ){
                return [NSString stringWithFormat:@"%d分钟前",s/60];
            }else {
                return [NSString stringWithFormat:@"%d小时",s/3600];
            }
        }else if ([currentDate isDateInYesterday:creatDate]){
            df.dateFormat = @"昨天 hh:mm";
        }else{
            df.dateFormat = @"MM月dd日 hh:mm";
        }
    }
    return [df stringFromDate:creatDate];
}
//判断是否是本年
- (BOOL)isCurrentYear:(NSDate*)date{
    NSDateFormatter* df = [[NSDateFormatter alloc]init];
    df.dateFormat = @"yyyy";
    NSString* sinaDateString = [df stringFromDate:date];
    NSString* currrentDateString = [df stringFromDate:[NSDate date]];
    
    return [sinaDateString isEqualToString:currrentDateString];
    
}


//认证判断
- (UIImage*) verifyImage:(int)verify{
    switch (verify) {
        case 1:
            return [UIImage imageNamed:@"avatar_vip"];
            case 2:
        case 3:
            case 5:
            return [UIImage imageNamed:@"avatar_enterprise_vip"];
            case 220:
            return [UIImage imageNamed:@"avatar_vgirl"];
        default:
            return [UIImage imageNamed:@"avatar_grassroot"];
    }
    
}
- (UIImage*) dealmBrank:(int)mbrank{
    
    if (mbrank>0 && mbrank<7){
        return  [UIImage imageNamed:[NSString stringWithFormat:@"common_icon_membership_level%d",mbrank]];
    }
    return [UIImage imageNamed:@"common_icon_membership"];
}

- (NSAttributedString*)originalAttr{
    
 return   [self getAttaribute:self.homeModel.text];
   
}

- (NSAttributedString*)reweetAttr{
    return [self getAttaribute:self.homeModel.retweeted_status.text];
}

- (NSAttributedString*)getAttaribute:(NSString*)text{
    
    NSMutableAttributedString* attrM = [[NSMutableAttributedString alloc]initWithString:text];
    
    
    NSRegularExpression* regular = [NSRegularExpression regularExpressionWithPattern:@"\\[[\\w]+\\]" options:0 error:nil];
     NSArray* checkingResults = [regular matchesInString: text options:0 range:NSMakeRange(0, attrM.length)];
    
    //遍历逆排序防止因为正向改变引起越界和修改范围出错
    ///替换emotion图片
    for ( NSTextCheckingResult* result in [checkingResults reverseObjectEnumerator]){
        //获取字符串
        NSString* res = [text substringWithRange:result.range ];
        //获取模型
        EPMEmotionModel* modle = [[EPMEmotionTool sheard] getEmotionModel:res];
        if (modle.allPath.length == 0)  continue;
        UIImage* img = [UIImage imageNamed:modle.allPath];
        
        NSAttributedString* attr = [NSMutableAttributedString yy_attachmentStringWithEmojiImage:img fontSize:FONTSIZENORMAL];
        
        [attrM replaceCharactersInRange:result.range withAttributedString:attr];
        
    }
    
    ///标识提到的人
    [self setHightPattern:@"@[a-zA-Z0-9\\u4e00-\\u9fa5_\\-]+" allAttributedString:attrM];
    ///话题
    [self setHightPattern:@"#[^#]+#" allAttributedString:attrM];
    ///网址
    [self setHightPattern:@"([hH]ttp[s]{0,1})://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\-~!@#$%^&*+?:_/=<>.',;]*)?" allAttributedString:attrM];
    [attrM addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:FONTSIZENORMAL] range:NSMakeRange(0, attrM.length)];
    return attrM;
}

- (NSMutableAttributedString*)setHightPattern:(NSString*)pattern allAttributedString:(NSMutableAttributedString*)allAttr{
    NSRegularExpression* regular = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
    NSArray* temp = [regular matchesInString:allAttr.string options:0 range:NSMakeRange(0, allAttr.length)];\
    //设置border
    YYTextBorder* border = [YYTextBorder borderWithFillColor:[UIColor lightGrayColor] cornerRadius:2];
    border.insets = UIEdgeInsetsMake(1, 0, 0, 0);
    //设置高亮
    YYTextHighlight* hightLight = [YYTextHighlight new];
    [hightLight setColor:[UIColor whiteColor]];
    [hightLight setBorder:border];
    
    for (NSTextCheckingResult* result in temp){
      
        [allAttr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:result.range];
        [allAttr  yy_setTextHighlight:hightLight range:result.range];
    }
    
    
    
    return  allAttr;
}

@end
